home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 1 / Amiga Tools.iso / wb-tools / toolmanager / source / library / handler.c < prev    next >
C/C++ Source or Header  |  1994-06-06  |  12KB  |  431 lines

  1. /*
  2.  * handler.c  V2.1
  3.  *
  4.  * handler main loop
  5.  *
  6.  * (c) 1990-1993 Stefan Becker
  7.  */
  8.  
  9. #include "ToolManagerLib.h"
  10.  
  11. /* misc. data */
  12. static BOOL TimerOpen=FALSE;
  13. struct Library *DOSBase=NULL;
  14. struct Library *CxBase=NULL;
  15. struct Library *UtilityBase=NULL;
  16. struct IntuitionBase *IntuitionBase=NULL;
  17. struct Library *GfxBase=NULL;
  18. struct Library *GadToolsBase=NULL;
  19. struct Library *NIPCBase=NULL;
  20. extern const char DosName[];
  21. extern struct Library *LibBase;
  22. static struct NotifyRequest PrefsNotify={
  23.                                          PrefsFileName,NULL,0,
  24.                                          NRF_SEND_SIGNAL
  25.                                         };
  26. static ULONG EntitySignal=-1;
  27. static ULONG NotifySignal=-1;
  28. static UBYTE *HostNameBuffer=NULL;
  29. #define HOSTNAMEBUFLEN 256
  30. static BOOL NotifyActive=FALSE;
  31.  
  32. /* Open handler resources */
  33. static BOOL OpenResources(void)
  34. {
  35.  if (!(LibraryPort=CreateMsgPort()))                       return(FALSE);
  36.  if (!(DOSBase=OpenLibrary(DosName,37)))                   return(FALSE);
  37.  if (!(CxBase=OpenLibrary("commodities.library",37)))      return(FALSE);
  38.  if (!(UtilityBase=OpenLibrary("utility.library",37)))     return(FALSE);
  39.  if (!(IntuitionBase=(struct IntuitionBase *)
  40.                       OpenLibrary("intuition.library",37))) return(FALSE);
  41.  if (!(GfxBase=OpenLibrary("graphics.library",37)))        return(FALSE);
  42.  if (!(GadToolsBase=OpenLibrary("gadtools.library",37)))   return(FALSE);
  43.  if (!(DummyPort=CreateMsgPort()))                         return(FALSE);
  44.  if (!(IDCMPPort=CreateMsgPort()))                         return(FALSE);
  45.  if (!(TimerPort=CreateMsgPort()))                         return(FALSE);
  46.  if (!(AppMsgPort=CreateMsgPort()))                        return(FALSE);
  47.  if (!(BrokerPort=CreateMsgPort()))                        return(FALSE);
  48.  if (!(deftimereq=CreateIORequest(TimerPort,sizeof(struct timerequest))))
  49.                                                            return(FALSE);
  50.  if (OpenDevice(TIMERNAME,UNIT_MICROHZ,(struct IORequest *) deftimereq,
  51.                 0))                                        return(FALSE);
  52.  TimerOpen=TRUE;
  53.  if (!(PrivateTMHandle=AllocMem(sizeof(struct TMHandle),MEMF_PUBLIC)))
  54.                                                            return(FALSE);
  55.  if (!(InternalAllocTMHandle(PrivateTMHandle)))            return(FALSE);
  56.  if ((NotifySignal=AllocSignal(-1))==-1)                   return(FALSE);
  57.  PrefsNotify.nr_stuff.nr_Signal.nr_Task=FindTask(NULL);
  58.  PrefsNotify.nr_stuff.nr_Signal.nr_SignalNum=NotifySignal;
  59.  if (!StartNotify(&PrefsNotify))                           return(FALSE);
  60.  NotifyActive=TRUE;
  61.  BrokerData.nb_Port=BrokerPort;
  62.  
  63.  /* Get locale stuff */
  64.  GetLocale();
  65.  
  66.  if (!(Broker=CxBroker(&BrokerData,NULL)))                 return(FALSE);
  67.  
  68.  /* Copy path from Workbench process */
  69.  {
  70.   struct Process *wbproc=(struct Process *) FindTask("Workbench");
  71.  
  72.   /* Task found? Make sure it IS a process */
  73.   if (wbproc && (wbproc->pr_Task.tc_Node.ln_Type==NT_PROCESS)) {
  74.    /* It is a process */
  75.    struct CommandLineInterface *wbcli=BADDR(wbproc->pr_CLI);
  76.  
  77.    /* Make sure it is a CLI process */
  78.    if (wbcli) {
  79.     /* It is a CLI process */
  80.     struct PathList *dummy;
  81.  
  82.     /* Copy it's path into global path */
  83.     if (!CopyPathList(&GlobalPath,&dummy,
  84.                       (struct PathList *) BADDR(wbcli->cli_CommandDir)))
  85.      return(FALSE);
  86.    }
  87.   }
  88.  }
  89.  
  90.  /* Get network stuff */
  91.  if (NIPCBase=OpenLibrary("nipc.library",37)) {
  92.   /* Create local public entity */
  93.   if (!(LocalEntity=CreateEntity(ENT_AllocSignal, &EntitySignal,
  94.                                  ENT_Name,        ToolManagerName,
  95.                                  ENT_Public,      TRUE,
  96.                                  TAG_DONE)))
  97.    return(FALSE);
  98.  
  99.   if (!(HostNameBuffer=AllocMem(HOSTNAMEBUFLEN,MEMF_PUBLIC))) return(FALSE);
  100.  }
  101.  
  102.  /* All OK. */
  103.  return(TRUE);
  104. }
  105.  
  106. /* Free handler resources */
  107. static void FreeResources(void)
  108. {
  109.  if (HostNameBuffer) {
  110.   FreeMem(HostNameBuffer,HOSTNAMEBUFLEN);
  111.   HostNameBuffer=NULL;
  112.  }
  113.  if (LocalEntity) {
  114.   DeleteEntity(LocalEntity);
  115.   LocalEntity=NULL;
  116.   EntitySignal=-1;
  117.  }
  118.  if (NIPCBase) {
  119.   CloseLibrary(NIPCBase);
  120.   NIPCBase=NULL;
  121.  }
  122.  
  123.  FreePathList(GlobalPath); /* FreePath checks for NULL */
  124.  GlobalPath=NULL;
  125.  
  126.  if (Broker) {
  127.   DeleteCxObjAll(Broker);
  128.   Broker=NULL;
  129.  }
  130.  FreeLocale();
  131.  if (NotifyActive) {
  132.   EndNotify(&PrefsNotify);
  133.   NotifyActive=FALSE;
  134.  }
  135.  if (NotifySignal != -1) {
  136.   FreeSignal(NotifySignal);
  137.   NotifySignal=-1;
  138.  }
  139.  if (PrivateTMHandle) {
  140.   InternalFreeTMHandle(PrivateTMHandle);
  141.   FreeMem(PrivateTMHandle,sizeof(struct TMHandle));
  142.   PrivateTMHandle=NULL;
  143.  }
  144.  if (TimerOpen) {
  145.   CloseDevice((struct IORequest *) deftimereq);
  146.   TimerOpen=FALSE;
  147.  }
  148.  if (deftimereq) {
  149.   DeleteIORequest(deftimereq);
  150.   deftimereq=NULL;
  151.  }
  152.  if (BrokerPort) {
  153.   struct Message *msg;
  154.   while (msg=GetMsg(BrokerPort)) ReplyMsg(msg);
  155.   DeleteMsgPort(BrokerPort);
  156.   BrokerPort=NULL;
  157.  }
  158.  if (AppMsgPort) {
  159.   struct Message *msg;
  160.   while (msg=GetMsg(AppMsgPort)) ReplyMsg(msg);
  161.   DeleteMsgPort(AppMsgPort);
  162.   AppMsgPort=NULL;
  163.  }
  164.  if (TimerPort) {
  165.   DeleteMsgPort(TimerPort);
  166.   TimerPort=NULL;
  167.  }
  168.  if (IDCMPPort) {
  169.   DeleteMsgPort(IDCMPPort);
  170.   IDCMPPort=NULL;
  171.  }
  172.  if (DummyPort) {
  173.   DeleteMsgPort(DummyPort);
  174.   DummyPort=NULL;
  175.  }
  176.  if (GadToolsBase) {
  177.   CloseLibrary(GadToolsBase);
  178.   GadToolsBase=NULL;
  179.  }
  180.  if (GfxBase) {
  181.   CloseLibrary(GfxBase);
  182.   GfxBase=NULL;
  183.  }
  184.  if (IntuitionBase) {
  185.   CloseLibrary((struct Library *) IntuitionBase);
  186.   IntuitionBase=NULL;
  187.  }
  188.  if (UtilityBase) {
  189.   CloseLibrary(UtilityBase);
  190.   UtilityBase=NULL;
  191.  }
  192.  if (CxBase) {
  193.   CloseLibrary(CxBase);
  194.   CxBase=NULL;
  195.  }
  196.  if (DOSBase) {
  197.   CloseLibrary(DOSBase);
  198.   DOSBase=NULL;
  199.  }
  200.  if (LibraryPort) {
  201.   DeleteMsgPort(LibraryPort);
  202.   LibraryPort=NULL;
  203.  }
  204. }
  205.  
  206. /* handler main entry point */
  207. __geta4 void HandlerEntry(void)
  208. {
  209.  ULONG sigmask,lpsig,ipsig,tpsig,apsig,bpsig,nwsig,npsig;
  210.  
  211.  /* Get resources */
  212.  if (!OpenResources()) {
  213.   FreeResources();
  214.   return;
  215.  }
  216.  
  217.  /* Read config */
  218.  if (!Closing) ReadConfig();
  219.  
  220.  /* Build signal masks */
  221.  lpsig=1L<<LibraryPort->mp_SigBit;
  222.  ipsig=1L<<IDCMPPort->mp_SigBit;
  223.  tpsig=1L<<TimerPort->mp_SigBit;
  224.  apsig=1L<<AppMsgPort->mp_SigBit;
  225.  bpsig=1L<<BrokerPort->mp_SigBit;
  226.  if (EntitySignal != -1)
  227.   nwsig=1L<<EntitySignal;
  228.  else
  229.   nwsig=0;
  230.  npsig=1L<<NotifySignal;
  231.  sigmask=ipsig|lpsig|tpsig|apsig|bpsig|nwsig|npsig|SIGBREAKF_CTRL_F;
  232.  
  233.  /* Activate broker */
  234.  ActivateCxObj(Broker,TRUE);
  235.  
  236.  /* Wait until the end...*/
  237.  while (!Closing || (LibBase->lib_OpenCnt>0)) {
  238.   ULONG gotsigs;
  239.  
  240.   /* Wait for events */
  241.   DEBUG_PUTSTR("Handler: Waiting for messages...\n")
  242.   gotsigs=Wait(sigmask);
  243.  
  244.   /* Got an IDCMP event? (only dock objects!) */
  245.   if (gotsigs & ipsig) HandleIDCMPEvents();
  246.  
  247.   /* Got a timer event? */
  248.   if (gotsigs & tpsig) {
  249.    struct List *l=&TimerPort->mp_MsgList;
  250.    struct TMTimerReq *tr;
  251.  
  252.    /* Scan message list (special treatment for I/O Requests!) */
  253.    while (tr=GetHead(l)) CallActivateTMObject(tr->tmtr_Link,NULL);
  254.   }
  255.  
  256.   /* Got a Workbench App* message? */
  257.   if (gotsigs & apsig) {
  258.    struct AppMessage *msg;
  259.  
  260.    /* Empty message queue */
  261.    while (msg=(struct AppMessage *) GetMsg(AppMsgPort)) {
  262.     /* Activate object */
  263.     CallActivateTMObject((struct TMLink *) msg->am_ID,msg);
  264.  
  265.     /* Reply message */
  266.     ReplyMsg((struct Message *) msg);
  267.    }
  268.   }
  269.  
  270.   /* Got a Commodities event? */
  271.   if (gotsigs & bpsig) {
  272.    CxMsg *msg;
  273.  
  274.    /* Empty message queue */
  275.    while (msg=(CxMsg *) GetMsg(BrokerPort)) {
  276.  
  277.     DEBUG_PRINTF("Commodities event (0x%08lx)\n",CxMsgType(msg));
  278.  
  279.     /* What type of Commodities event? */
  280.     switch (CxMsgType(msg)) {
  281.      case CXM_IEVENT: /* Hotkey event --> Activate object */
  282.                      CallActivateTMObject((struct TMLink *) CxMsgID(msg),
  283.                                           NULL);
  284.                      break;
  285.  
  286.      case CXM_COMMAND: /* Commodities command */
  287.                       switch (CxMsgID(msg)) {
  288.                        case CXCMD_DISABLE: /* Disable broker */
  289.                                           ActivateCxObj(Broker,FALSE);
  290.                                           break;
  291.  
  292.                        case CXCMD_ENABLE:  /* Enable broker */
  293.                                           ActivateCxObj(Broker,TRUE);
  294.                                           break;
  295.  
  296.                        case CXCMD_KILL:    /* Quit application */
  297.                                           Closing=TRUE;
  298.                                           break;
  299.                        }
  300.                       break;
  301.     }
  302.  
  303.     /* Reply message */
  304.     ReplyMsg((struct Message *) msg);
  305.    }
  306.   }
  307.  
  308.   /* Got a network event? */
  309.   if (gotsigs & nwsig) {
  310.    struct Transaction *trans;
  311.  
  312.    /* Empty transaction queue */
  313.    while (trans=GetTransaction(LocalEntity)) {
  314.     struct TMLink *tml=NULL;
  315.     ULONG errcode=ENVOYERR_APP;
  316.  
  317.     /* Get host name of remote machine */
  318.     if (GetHostName(trans->trans_SourceEntity,HostNameBuffer,HOSTNAMEBUFLEN))
  319.  
  320.      /* Search Access object (with full host name) */
  321.      if (!(tml=AddLinkTMObject(PrivateTMHandle,HostNameBuffer,TMOBJTYPE_ACCESS,
  322.                                NULL))) {
  323.       char *realm;
  324.  
  325.       /* Locate realm part */
  326.       if (realm=strchr(HostNameBuffer,':')) {
  327.        /* Set string terminator */
  328.        *realm='\0';
  329.  
  330.        /* Search Access object (with realm name) */
  331.        tml=AddLinkTMObject(PrivateTMHandle,HostNameBuffer,TMOBJTYPE_ACCESS,
  332.                                NULL);
  333.       }
  334.      }
  335.  
  336.     DEBUG_PRINTF("Host name: '%s'\n",HostNameBuffer);
  337.  
  338.     /* Object not found? */
  339.     if (!tml)
  340.      /* Search default Access object */
  341.      tml=AddLinkTMObject(PrivateTMHandle,"anyone",TMOBJTYPE_ACCESS,NULL);
  342.  
  343.     DEBUG_PRINTF("Link: 0x%08lx\n",tml);
  344.  
  345.     /* Object found? */
  346.     if (tml) {
  347.      /* Yes, activate Access object */
  348.      CallActivateTMObject(tml,trans->trans_RequestData);
  349.  
  350.      /* Remove link */
  351.      RemLinkTMObject(tml);
  352.  
  353.      /* All OK (hope so :-) */
  354.      errcode=ENVOYERR_NOERROR;
  355.     }
  356.  
  357.     /* Reply transaction */
  358.     trans->trans_Error=errcode;
  359.     ReplyTransaction(trans);
  360.    }
  361.   }
  362.  
  363.   /* Got a command from the library interface? */
  364.   if (gotsigs & lpsig) {
  365.    struct TMHandle *handle;
  366.  
  367.    /* Empty message queue */
  368.    while (handle=(struct TMHandle *) GetMsg(LibraryPort)) {
  369.     BOOL rc=FALSE;
  370.  
  371.     DEBUG_PRINTF("Got command (%1lx)\n",handle->tmh_Command);
  372.  
  373.     /* What command did we receive? */
  374.     switch (handle->tmh_Command) {
  375.      case TMIPC_AllocTMHandle:  rc=InternalAllocTMHandle(handle);
  376.                                 break;
  377.  
  378.      case TMIPC_FreeTMHandle:   rc=InternalFreeTMHandle(handle);
  379.                                 break;
  380.  
  381.      case TMIPC_CreateTMObject: rc=InternalCreateTMObject(handle,
  382.                                                           handle->tmh_Object,
  383.                                                           handle->tmh_Type,
  384.                                                           handle->tmh_Tags);
  385.                                 break;
  386.  
  387.      case TMIPC_DeleteTMObject: rc=InternalDeleteTMObject(handle,
  388.                                                           handle->tmh_Object);
  389.                                 break;
  390.  
  391.      case TMIPC_ChangeTMObject: rc=InternalChangeTMObject(handle,
  392.                                                           handle->tmh_Object,
  393.                                                           handle->tmh_Tags);
  394.                                 break;
  395.     }
  396.  
  397.     /* Reply message */
  398.     handle->tmh_Command=rc;
  399.     DEBUG_PRINTF("Command reply (%1lx)\n",handle->tmh_Command);
  400.     ReplyMsg((struct Message *) handle);
  401.    }
  402.   }
  403.  
  404.   /* Got a configuration file notification event? */
  405.   if (gotsigs & npsig) {
  406.  
  407.    DEBUG_PRINTF("Preferences changed!\n");
  408.  
  409.    /* Free old config */
  410.    InternalFreeTMHandle(PrivateTMHandle);
  411.    FreeConfig();
  412.  
  413.    /* Read new config */
  414.    ReadConfig();
  415.   }
  416.  }
  417.  
  418.  /* Deactivate broker */
  419.  ActivateCxObj(Broker,FALSE);
  420.  
  421.  /* Free config buffers */
  422.  InternalFreeTMHandle(PrivateTMHandle);
  423.  FreeConfig();
  424.  
  425.  /* Free all resources &  Reset closing flag */
  426.  DEBUG_PUTSTR("Handler signing off...\n")
  427. /* Forbid(); */
  428.  FreeResources();
  429.  Closing=FALSE;
  430. }
  431.